package dao;

import java.util.List;

import org.hibernate.SessionFactory;

import bean.Customer;
import bean.Employee;
import bean.LoggedUser;
import bean.User;
import bean.UserBean;

public class LoginDAO extends DAO<UserBean> {

	public LoginDAO(SessionFactory factory) {
		super(factory);
	}

	public LoggedUser loginUser(User dbuser)
	{
		LoggedUser user = null;
		String name = dbuser.getUsername();
		if(dbuser.isCustomer())
		{
			List customers = this.getByAttribute("username", name, Customer.class);
			if(customers != null && customers.size()>0)
			{
				Customer customer = (Customer)customers.get(0);
				if(customer.getUsername().equals(name))
				{
					user = LoggedUser.createFromCustomer(customer);
				}
			}
		}
		else
		{
			List employees = this.getByAttribute("username", name, Employee.class);
			if(employees!=null)
			{
				Employee employee = (Employee)employees.get(0);
				if(employee.getUsername().equals(name))
				{
					user = LoggedUser.createFromEmployee(employee);
				}
			}
		}
		return user;
	}

}
